﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using System.Data;

#endregion

namespace Rio.Doc.Business.Repository.Translator
{
    /// <summary>
    /// Class used to convert Group database row to Group object
    /// </summary>
    public class GroupTranslator
    {
            /// <summary>
            /// Translate a bunch of Group DataReader to Group object
            /// </summary>
            /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
            /// <returns>A list of Group Object</returns>
            public static Group[] TranslateGroup(IDataReader P_oDbDataReader)
            {
                var l_lstGroups = new List<Group>();
                try
                {
                    using (P_oDbDataReader)
                    {
                        while (P_oDbDataReader.Read())
                        {
                            var l_oGroup = new GroupRepository();

                            l_oGroup.Identifier = Convert.ToInt32(P_oDbDataReader["RG_ID"].ToString());
                            l_oGroup.Name = P_oDbDataReader["RG_NOM"].ToString();
                            l_oGroup.CanConsult =
                                Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_CONS"].ToString()));
                            l_oGroup.CanReorganize =
                                Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_REORG"].ToString()));
                            l_oGroup.IsAdministrator =
                                Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_ADMIN"].ToString()));
                            l_oGroup.CanScann =
                                Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_NUM"].ToString()));
                            l_oGroup.CanPrint =
                                Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_IMPRESSION"].ToString()));
                            l_oGroup.CanDownload =
                                Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_TELECHARGER"].ToString()));
                            l_oGroup.CanSendEmail =
                                Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_EMAIL"].ToString()));

                            if (P_oDbDataReader["RG_FLOW"] != DBNull.Value)
                            {
                                l_oGroup.CanFlow =
                                    Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_FLOW"].ToString()));
                            }
                            else
                            {
                                l_oGroup.CanFlow = true;
                            }

                            if (P_oDbDataReader["RG_MASSIMPORT"] != DBNull.Value)
                            {
                                l_oGroup.CanMassImport =
                                    Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_MASSIMPORT"].ToString()));
                            }
                            else
                            {
                                l_oGroup.CanMassImport = false;
                            }

                            try
                            {
                                l_oGroup.CanDelete =
                                       Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_DELETE"].ToString()));
                            }
                            catch (Exception)
                            {
                                //Old compatibility
                                l_oGroup.CanDelete = l_oGroup.CanReorganize;
                            }

                            try
                            {
                                l_oGroup.CanFlowValidation =
                                       Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_FLOWVALID"].ToString()));
                            }
                            catch (Exception)
                            {
                                l_oGroup.CanFlowValidation = false;
                            }

                            //Trash item right
                            try
                            {
                                l_oGroup.CanTrash =
                                       Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_REMOVE"].ToString()));
                            }
                            catch (Exception)
                            {
                                l_oGroup.CanTrash = false;
                            }

                            l_lstGroups.Add(l_oGroup);
                        }
                    }
                }
                catch (Exception ex)
                {
                    NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
                }

                return l_lstGroups.ToArray();
            }
        }
    
}
